package com.cashmate.struts;

import com.cashmate.hibernate.dao.RequirementDAO;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Requirements;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class EditRequirements extends ActionSupport {

    private static Logger logger = Logger.getLogger(EditRequirements.class);
    private String oper;
    private String id;
    private String requirementname;
    private RequirementDAO rqdao = (RequirementDAO) ServletActionContext.getServletContext().getAttribute("requirementDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    @Action(value = "/edit-requirements", results = {
        @Result(name = "duplicate", location = "../error/formError.jsp"),
        @Result(name = "error", location = "../error/messages.jsp")
    })
    @Override
    public String execute() {
        try {
            Requirements r = null;
            if (oper.equals("add")) {
                if (rqdao.hasDuplicate(requirementname)) {
                    addActionError("ERROR : Requirement " + requirementname + " already exists.");
                    return "duplicate";
                }
                r = new Requirements(requirementname, true);
                rqdao.insert(r);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") added a new Requirement: {requiremenrid:" + id + ", requirementname:" + requirementname + "}");
            } else if (oper.equals("edit")) {
                r = rqdao.get(Integer.parseInt(id));
                if (!r.getRequirementname().equals(requirementname)) {
                    if (rqdao.hasDuplicate(requirementname)) {
                        addActionError("ERROR : Requirement " + requirementname + " already exists.");
                        return "duplicate";
                    }
                }
                String old = r.getRequirementname();
                r.setRequirementname(requirementname);
                rqdao.update(r);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Requirement(" + old + ") with new values: {requirementname:" + requirementname + "}");
            } else if (oper.equals("del")) {
                r = rqdao.get(Integer.parseInt(id));
                r.setIsactive(false);
                rqdao.update(r);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") deleted Requirement(" + r.getRequirementname() + ")");
            }
        } catch (Exception e) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
            }
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return NONE;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setRequirementname(String requirementname) {
        this.requirementname = requirementname;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }
}
